package cn.jhc.service;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.dao.Dao;

@WebServlet("/login2.do")
public class LoginService extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        boolean isValid = false;
        try {
            isValid = new Dao().login(username, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(isValid) {
            System.out.println("login:" + username);
            req.getSession().setAttribute("logining_use", username);
            resp.sendRedirect("/index.html?username=" + username);
        }else {
            resp.sendRedirect("/login2.html?mes=login_fail");
        }

        
    }
}
